<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>字典管理</title>
<jsp:include page="/WEB-INF/inc/systemInc.jsp"></jsp:include>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/addDictionary')}">
  <script type="text/javascript">
			$.canAdd = true;
		</script>
</c:if>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/editDictionaryPage')}">
  <script type="text/javascript">
			$.canEdit = true;
		</script>
</c:if>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/delete')}">
  <script type="text/javascript">
			$.canDelete = true;
		</script>
</c:if>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/jslib/syUtil.js">
	
</script>
<script type="text/javascript">
	var treeGrid;
	// 初始化datagrid
	$(function() {
		// 初始化数据
		treeGrid = $('#treeGrid').treegrid(
				{
//   				 	loadFilter:function(data){return undo();},
					url : '${pageContext.request.contextPath}/admin/dictionary/dictionaryDataGrid',
					idField : 'id',
					treeField : 'dictName',
					parentField : 'pid', 
					initialState : "collapsed",
					fit : true,
					fitColumns : true,
					border : false,
					pagination : false,
					singleSelect : true,
					columns : [ [
						{
							title : 'id',
							field : 'id',
							width : 50,
							checkbox : true,
							hidden : true
						},
						{
							field : 'dictName',
							title : '字典名称',
// 							state:'closed',
							width : 150
						},
						{
							field : 'dictCode',
							title : '字典编号',
							width : 150
						},
						{
							field : 'dictSort',
							title : '排序',
							width : 150
						},
						{
							field : 'dictDesc',
							title : '备注',
							width : 250
						},
						{
							field : 'action',
							title : '操作',
							width : 100,
							formatter : function(value, row, index) {
								var str = '';
// 								if ($.canEdit) {
									str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', row.id,
											'${pageContext.request.contextPath}/resources/css/images/extjs_icons/pencil.png');
// 								}
								str += '&nbsp;';
// 								if ($.canDelete) {
									str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id,
											'${pageContext.request.contextPath}/resources/css/images/extjs_icons/delete.png');
// 								}
								return str;
							}
						} ] ],
					toolbar : '#toolbar',
					onContextMenu : function(e, row) {
						e.preventDefault();
						$(this).treegrid('unselectAll');
						$(this).treegrid('select', row.id);
						$('#menu').menu('show', {
							left : e.pageX,
							top : e.pageY
						});
					},
					onLoadSuccess : function() {
					  $('#dataGrid').treegrid('collapseAll');
						parent.$.messager.progress('close');

						$(this).datagrid('tooltip');
// 						
					}
					,
					onLoadError : function()
					{
				        $.messager.confirm('提示', '您未登录或者长时间没有操作，请重新登录！', function(result){
				                if (result){
				                  parent.location.href='${pageContext.request.contextPath}/adminlogin.jsp';
				                }
				            });
					}
				});

	});

	// 添加字典
	function addFun() {
		parent.$.modalDialog({
			title : '添加字典项',
			width : 500,
			height : 240,
			href : '${pageContext.request.contextPath}/admin/dictionary/addPage',
			buttons : [ {
				text : '添加',
				handler : function() {
					parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

//判断是否有子集，若有子集将子集收起
	function TreeGridShow(data) {
 			 $(data).each(function (index, item) {
      		if (item.pid=='0') {
//           			 item.state = "open";
      		  item.state = "closed";
      			}
      			else {
      			  treeGrid.treegrid('collapseAll');
          			 item.state = "open";
//           			 TreeGridShow(item.children);
     				 }
 				 });
  				return data;
		}

	// 修改字典
	function editFun(id) {
		if (id != undefined) {
			treeGrid.treegrid('select', id);
		}
		var node = treeGrid.treegrid('getSelected');
		if (node) {
			parent.$.modalDialog({
				title : '修改字典',
				width : 500,
				height : 240,
				href : '${pageContext.request.contextPath}/admin/dictionary/editDictionaryPage?id=' + node.id
						+ '&parentId=' + node.pid,
				buttons : [ {
					text : '修改',
					handler : function() {
						parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#form');
						f.submit();
					}
				} ]
			});
		}
	}

	// 删除角色
	function deleteFun(id) {
		if (id != undefined) {
			treeGrid.treegrid('select', id);
		}
		var node = treeGrid.treegrid('getSelected');
		if (node) {
			parent.$.messager.confirm('确认', '您确定要删除所选字典吗？', function(b) {
				if (b) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post('${pageContext.request.contextPath}/admin/dictionary/deleteDictionary', {
						id : node.id,
						parentId : node.parentId
					}, function(result) {
						if (result.obj == "success") {
							parent.$.messager.alert('成功', result.msg, 'info');
							treeGrid.treegrid('reload');
						} else if (result.obj == "") {
							parent.$.messager.alert('错误', result.msg, 'error');
						} else {
							parent.$.messager.alert('警告', result.msg, 'warning');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
				}
			});
		}
	}

	// 展开菜单
	function redo() {

		var node = treeGrid.treegrid('getSelected');
		if (node) {
			treeGrid.treegrid('expandAll', node.id);
		} else {
			treeGrid.treegrid('expandAll');
		}
	}

	// 折叠菜单
	function undo() {
		var node = treeGrid.treegrid('getSelected');
		if (node) {
			treeGrid.treegrid('collapseAll', node.id);
		} else {
			treeGrid.treegrid('collapseAll');
		}
	}
</script>
</head>
<body>
  <div id="toolbar" style="display: none;">
<%--     <c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/addDictionary')}"> --%>
      <a href="javascript:void(0);" onclick="addFun();" class="easyui-linkbutton"
        data-options="plain:true,iconCls:'pencil_add'">增加</a>
<%--     </c:if> --%>
    <a onclick="redo();" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'resultset_next'">展开</a> <a onclick="undo();" href="javascript:void(0);"
      class="easyui-linkbutton" data-options="plain:true,iconCls:'resultset_previous'">折叠</a><a
      onclick="treeGrid.treegrid('reload');" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'database_refresh'">刷新</a>
  </div>

  <div class="easyui-layout"  data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
      <table id="treeGrid"></table>
    </div>
  </div>
  <div id="menu" class="easyui-menu" style="width: 120px; display: none;">
    <c:if test="${fn:contains(sessionInfo.moduleList, '/dictionaryController/editDictionaryPage')}">
      <div onclick="editFun();" data-options="iconCls:'pencil'">修改</div>
    </c:if>
    <c:if test="${fn:contains(sessionInfo.moduleList, '/dictionaryController/deleteDictionary')}">
      <div onclick="deleteFun();" data-options="iconCls:'delete'">删除</div>
    </c:if>
  </div>

</body>
</html>